import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
// import * as hooks from './hooks'




//解决面包屑点击上一级路由报错的bug,高版本不会报错
let originPush = VueRouter.prototype.push //备份原push方法
let originReplace = VueRouter.prototype.replace //备份原push方法

VueRouter.prototype.push = function (location, resolve, reject) {
  if (resolve && reject) {
    //如果传了回调函数，直接使用
    originPush.call(this, location, resolve, reject)
  } else {
    //如果没有传回调函数，手动添加
    originPush.call(
      this,
      location,
      () => {},
      () => {}
    )
  }
}

VueRouter.prototype.replace = function (location, resolve, reject) {
  if (resolve && reject) {
    //如果传了回调函数，直接使用
    originReplace.call(this, location, resolve, reject)
  } else {
    //如果没有传回调函数，手动添加
    originReplace.call(
      this,
      location,
      () => {},
      () => {}
    )
  }
}


import sys from './routes/sys'
import dashoard from './routes/dashoard'
const moduleFn = require.context("./routes", false, /\.js$/i)
const children = moduleFn.keys().reduce((prev, path) => {
  const value = moduleFn(path).default
  if (Array.isArray(value)) {
    prev.push(...value)
  } else {
    prev.push(value)
  }
  return prev
}, [])

Vue.use(VueRouter)

// 定义路由规则表
const routes = [
  {
    path: '/',
    component: () => import('@/views/login'),
    meta: {
      nologin: true
    },
    // redirect: "/"
  },
  {
    path: "/login",
    component: () => import("@/views/login"),
    redirect: "/login"
  },
  {
    path: '/admin',
    component: () => import('@/views/admin'),
    redirect: '/dashoard',
    // children: [...sys, ...dashoard]
    children
  }
]

const router = new VueRouter({
  mode: 'history',
  base: "/",
  routes
})



export default router
